# Crossin的编程教室
import requests
from bs4 import BeautifulSoup
from threading import Thread

def down_pic(link):
    print('downloading', link)
    filename = link.split('/')[-1]

    retires = 0
    while retires < 3:
        try:
            # 这里需加上 headers 参数，具体原因后续课程中会讲解
            pic = requests.get(link, headers={'referer':'http://www.cxdq.com/'}, timeout=15)
            with open('pics/' + filename, 'wb') as f:
                f.write(pic.content)
        except requests.exceptions.RequestException as e:
            retires += 1
            print(e)
            print(filename, 'failed')
        else:
            print(filename, 'saved')
            break

# url = 'http://jandan.net/pic'
# 抓图网址改为 搞笑图片 cxdq.com
url = 'http://www.cxdq.com/index.htm'
url_base = 'http://www.cxdq.com/index_%d.htm'

for i in range(1, 2):
    if i > 1:
        url = url_base % i
    print(url)
    # 这里需加上 headers 参数，具体原因后续课程中会讲解
    req = requests.get(url, headers={'referer':'http://www.cxdq.com/'})
    html = req.text

    soup = BeautifulSoup(html, 'lxml')
    # result = soup.find_all('a', class_='view_img_link')
    # 提取出img标签的,以list返回
    content = soup.find('div', class_='thumbarea')
    result = content.find_all('img')
    for link in result:
        # link = link.get('href')
        # 提取出img标签的src
        link = link.get('src')
        t = Thread(target=down_pic, args=(link,))
        t.start()

    # current_page = soup.find_all('span', class_='current-comment-page')
    # next_page = int(current_page.strip('[]')) - 1
